<template>
  <div v-if="this.itemArr" class="new-activity" :class="[{'empty':showEmpty}]">
    <s-activity v-if="showSkeleton"></s-activity>
    <div v-else class="content">
        <ul v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="20" infinite-scroll-immediate-check="false">
            <router-link tag="li" v-for="(item,index) in itemArr" :to=" 'information/' + item.id" :key="item.id" :class="[{'notLast':index!=itemArr.length-1}]">
              <div class="left">
                  <div class="name">{{item.itemName}}</div>
                  <div class="time">{{item.endTime}}</div>
              </div>
              <my-image class="my-img fade" :imgUrl="item.imgUrl"></my-image>
            </router-link>
        </ul>
        <div v-if="showEmpty" class="div-empty">
            <span class="iconfont icon-meiyougengduole"></span>
            <p class="empty-tip">暂无最新资讯~</p> 
        </div>
    </div>
  </div>
</template>

<script>
import { formatDate } from "../../../common/js/date.js";
import { mapState } from "vuex";
import myImage from "../../lazyloadImg/lazyImage.vue";
import Vue from "vue";
import { InfiniteScroll, Toast } from "mint-ui";
import "mint-ui/lib/toast/style.css";
import sActivity from "../../skeletons/sActivity.vue";
Vue.use(InfiniteScroll);
Vue.component(Toast.name, Toast);

export default {
  name: "new-activity",
  data() {
    return {
      showEmpty: false,
      itemArr: [],
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      showSkeleton: true
    };
  },
  components: {
    myImage,
    sActivity
  },
  activated() {},
  mounted() {
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      if(this.$route.query&&this.$route.query.tabIndex!=0) return;
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Goods/goods_list", {
          type: "4",
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (infinite && this.curPage == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (this.showSkeleton) this.showSkeleton = false;
          let serverData = response.body.result.arr;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) {
              this.showEmpty = true;
            }
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              //if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.itemArr.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        let tempDate = new Date(val.addtime * 1000);
        let happenTime = formatDate(tempDate, "yyyy-MM-dd hh:mm");
        detailsArr.push({
          id: val.id,
          type: "0",
          imgUrl: this.soursePath + val.icon,
          itemName: val.gname,
          price: val.price,
          dec: val.message,
          discount: val.discount,
          endTime: happenTime
        });
      });
      return detailsArr;
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid", "soursePath"])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.new-activity {
  font-family: 'Microsoft YaHei';
  width: 100%;
  flex: 1;
  background-color: #efeff0;

  &.empty {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
  }

  .content {
    ul {
      padding-top: 0.08rem;

      li {
        width: 100%;
        min-height: 0.7rem;
        padding: 0.08rem 0.12rem;
        box-sizing: border-box;
        display: flex;
        background-color: #fff;

        &.notLast {
          border-bottom: 1px #ddd dashed;
        }

        .my-img {
          align-self center;
          background-color: #f1f1f1;
          // border-radius: 0.04rem;
          width: .9rem;
          height: .45rem;
          margin-left: 0.14rem;
          flex-shrink 0;
        }

        .left {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .name {
            margin-top .08rem;
            color: #000;
            font-size: 0.13rem;
          }

          .time {
            color: #999;
            font-size: 0.12rem;
            margin-bottom .08rem;
          }
        }
      }
    }

    .div-empty {
      .icon-meiyougengduole {
        font-size: 1.3rem;
        color: #595656;
      }

      .empty-tip {
        margin-top: 0.08rem;
        font-size: 0.15rem;
        text-align: center;
        color: #595656;
        letter-spacing: 0.01rem;
      }
    }
  }
}
</style>
